package com.example.springbootjdbc.controller;

import com.example.springbootjdbc.pojo.User;
import com.example.springbootjdbc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;
import java.util.List;

/**
 * Description:springbootweb
 * Created by admin  on 2020/4/17  15:09
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 添加用户
     * @return
     */
    @PostMapping("/addUser")
    public String  addUser( User user){
        try {
            userService.addUser(user);
            System.out.println("添加了一个用户");
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
        return "redirect:/success";
    }

    /**
     * 查询所有用户
     * @return
     */
    @GetMapping(value = "findUser")
    public String findAll(Model model){
        List<User> userList = null;
        try {
            userList = userService.findAll();
            model.addAttribute("userList",userList);
            System.out.println("查询了所有用户");
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
        return "showUsers";
    }

}
